using System;
using Gadgeteer.Interfaces.Network;
using Gadgeteer.Modules;

namespace Gadgeteer.Interfaces.Fakes
{
    public class FakeNetworkModule : INetworkModule
    {
        private bool connected;

        public FakeNetworkModule()
            : this(true)
        {
        }

        public FakeNetworkModule(bool connected)
        {
            this.connected = connected;
        }

        public bool IsNetworkConnected
        {
            get { return connected; }
        }

        public bool IsNetworkUp
        {
            get { return connected; }
        }

        public event NetworkEventHandler NetworkDown;

        private void OnNetworkDown()
        {
            var ev = NetworkDown;
            if (ev != null)
                ev(this, Module.NetworkModule.NetworkState.Down);
        }

        public event NetworkEventHandler NetworkUp;

        private void OnNetworkUp()
        {
            var ev = NetworkUp;
            if (ev != null)
                ev(this, Module.NetworkModule.NetworkState.Up);
        }

        #region Network settings
        public INetworkInterface NetworkSettings
        {
            get { return networkSettings; }
        }

        private readonly FakeNetWorkInterface networkSettings = new FakeNetWorkInterface();
        public class FakeNetWorkInterface : INetworkInterface
        {
            private string ipAddress;
            private string subnetMask;
            private string gatewayAddress;
            private string[] dnsAddresses;

            public FakeNetWorkInterface()
            {
                NetworkInterfaceType = Network.NetworkInterfaceType.Ethernet;
                IsDhcpEnabled = true;
                IsDynamicDnsEnabled = false;
                dnsAddresses = new[] { "192.168.1.1" };
            }

            public string[] DnsAddresses { get { return dnsAddresses; } }

            public string GatewayAddress { get { return gatewayAddress; } }

            public string IPAddress { get { return ipAddress; } }

            public string SubnetMask { get { return subnetMask; } }

            public bool IsDhcpEnabled { get; private set; }

            public bool IsDynamicDnsEnabled { get; private set; }

            public NetworkInterfaceType NetworkInterfaceType { get; private set; }

            public byte[] PhysicalAddress { get; set; }

            public void EnableDhcp()
            {
                IsDhcpEnabled = true;
                ipAddress = "192.168.1.98";
            }

            public void EnableDynamicDns()
            {
            }

            public void EnableStaticDns(string[] dnsAddresses)
            {
                this.dnsAddresses = dnsAddresses;
            }

            public void EnableStaticIP(string ipAddress, string subnetMask, string gatewayAddress)
            {
                this.ipAddress = ipAddress;
                this.subnetMask = subnetMask;
                this.gatewayAddress = gatewayAddress;
            }

            public void ReleaseDhcpLease()
            {
            }

            public void RenewDhcpLease()
            {
            }
        }

        public void UseDHCP()
        {
            networkSettings.EnableDhcp();
        }

        public void UseStaticIP(string IPAddress, string SubnetMask, string GatewayAddress)
        {
            networkSettings.EnableStaticIP(IPAddress, SubnetMask, GatewayAddress);
        }

        public void UseStaticIP(string IPAddress, string SubnetMask, string GatewayAddress, string[] DnsAddresses)
        {
            networkSettings.EnableStaticIP(IPAddress, SubnetMask, GatewayAddress);
            networkSettings.EnableStaticDns(DnsAddresses);
        }
        #endregion
    }
}
